<template>
  <div class="head">
    <div class="tit">
      <button
        id="1"
        :class="id === 1 ? 'active' : ''"
        class="tex1"
        @click="tab(1)"
      >
        科目汇总
      </button>
      <button
        id="2"
        :class="id === 2 ? 'active' : ''"
        class="tex1"
        @click="tab(2)"
      >
        费用子项
      </button>
    </div>
    <div class="line" />
    <el-row>
      <el-col :span="24">
        <div class="title1">
          <el-form
            ref="form"
            :rules="rules"
            style="
              display: flex;
              flex-wrap: wrap;
              margin-top: 24px;
              justify-content: space-between;
            "
            :model="form"
            label-width="80px"
          >
            <el-form-item
              style="margin-left: 0"
              class="text"
              label="日期"
              prop="Time"
            >
              <el-date-picker
                v-model="form.Time"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                style="width: 694px"
                @change="changeTime"
              />
            </el-form-item>
            <el-form-item class="text" label="时间汇总" prop="collectType">
              <el-select
                v-model="form.collectType"
                placeholder="请选择"
                style="width: 286px"
              >
                <el-option label="按天" value="1" />
                <el-option label="按时间段" value="2" />
              </el-select>
            </el-form-item>
            <el-form-item class="text" label="收付类型" prop="payReceiveType">
              <el-select
                v-model="form.payReceiveType"
                placeholder="请选择"
                style="width: 286px"
                clearable
                @change="chan"
              >
                <el-option label="向寄件方收" value="1" />
                <el-option label="向派件方付" value="2" />
              </el-select>
            </el-form-item>
            <div>
              <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
                <elbutton :obj="form" @dj="khClear" />
              </el-form-item>
            </div>

            <el-form-item class="text" label="网点名称">
              <el-select
                v-model="form.networkName"
                class="input"
                placeholder="辽宁盘锦公司"
                style="width: 286px"
                :disabled="true"
              >
                <el-option label="辽宁盘锦公司" value="1" />
              </el-select>
            </el-form-item>
            <el-form-item class="text" label="展示级别" prop="displayLevel">
              <el-select
                v-model="form.displayLevel"
                class="input"
                placeholder="请选择"
                style="width: 286px"
              >
                <el-option label="网点" value="1" />
                <el-option label="承包区" value="2" />
              </el-select>
            </el-form-item>
            <el-form-item class="text" label="承包区名">
              <el-select
                v-model="form.contractAreaNameId"
                class="input"
                placeholder="请选择"
                style="width: 286px"
              >
                <el-option
                  v-for="item in ShowCoverAreaList"
                  :key="item.id"
                  :label="item.networkName"
                  :value="item.networkName"
                />
              </el-select>
            </el-form-item>
            <el-form-item v-if="id === 2" class="text" label="结算科目">
              <el-select
                v-model="settleSubjectId"
                class="input"
                placeholder="请选择"
                style="width: 286px"
              >
                <el-option
                  v-for="item in SettlementList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <!-- <el-button type="primary" @click="onSubmit">查询</el-button> -->

              <el-button @click="onSubmit">
                <i class="el-icon-upload2" />
                导出
              </el-button>
            </el-form-item>
            <el-form-item v-if="id === 2" class="text" label="费用子项">
              <el-select
                v-model="channelId"
                class="input"
                placeholder="请选择"
                style="width: 286px"
              >
                <el-option
                  v-for="item in FeeSubItemList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <elltable v-if="id === 1" :loading="loading" :data="tableData" :th="th">
          <template v-slot:before-columns>
            <el-table-column
              v-if="tableData.length > 0"
              label="序号"
              type="index"
              :index="table_index"
              width="100"
              align="center"
            />
          </template>
        </elltable>
        <elltable v-if="id === 2" :loading="loading" :data="tableData" :th="th">
          <template v-slot:before-columns>
            <el-table-column
              v-if="tableData.length > 0"
              label="序号"
              type="index"
              :index="table_index"
              width="100"
              align="center"
            />
          </template>
        </elltable>
        <div
          style="
            display: flex;
            justify-content: space-between;
            margin-top: 51px;
          "
        >
          <div class="title2" />
          <div class="block" style="display: flex; justify-content: flex-end">
            <pagination
              :current-page="current"
              :page-size="10"
              :total="+total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  ShowCoverAreaApi,
  ToSenderApi,
  FeeSubItemApi1,
  SettlementApi,
  ExpenseSubitemApi
} from '@/api/HqFinancePanel'
import { getCityId, getProvinceList } from '@/api/dashboard'

export default {
  components: {},
  data() {
    return {
      rules: {
        Time: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ],
        collectType: [
          { required: true, message: '时间汇总', trigger: 'change' }
        ],
        payReceiveType: [
          { required: true, message: '请选择收寄方', trigger: 'change' }
        ],
        displayLevel: [
          { required: true, message: '请选择展示级别', trigger: 'change' }
        ]
      },
      th: [],
      id: 1,
      ProvinceList: [], // 省
      cityList: [], // 市
      value: '',
      value1: '',
      form: {
        Time: [],
        collectType: '',
        contractAreaNameId: '',
        payReceiveType: '',
        displayLevel: '',
        endTime: '',
        pageDate: {
          pageNum: 1,
          pageSize: 10
        },
        startTime: '',
        networkName: '辽宁盘锦公司'
      },
      mxForm: {
        destinationProvince: '',
        destinationCity: ''
      },
      tableData: [],
      ShowCoverAreaList: [], // 承包区列表
      current: 0, // 页数
      pages: 0, // 条数
      total: 0, // 总数
      elementHeight: 1180,
      FeeSubItemList: [], // 费用子项列表
      SettlementList: [], // 结算科目列表
      channelId: '', // 费用子项
      settleSubjectId: '', // 结算科目
      loading: false
    }
  },

  computed: {},

  created() {
    this.getProvince()
    this.ShowCoverArea()
    this.table_index()
    this.FeeSubItem()
    this.Settlement()
  },

  methods: {
    handleSizeChange(val) {
      this.form.pageDate.pageSize = val
      this.onSubmit()
    },
    handleCurrentChange(val) {
      this.form.pageDate.pageNum = val
      this.onSubmit()
    },
    // tab切换
    tab(data) {
      // console.log(data)
      this.id = data
      this.khClear()
      if (data === 2) {
        this.elementHeight = 1590
      } else {
        this.elementHeight = 1180
      }
    },
    // 序号
    table_index(index) {
      return (
        (this.form.pageDate.pageNum - 1) * this.form.pageDate.pageSize +
        index +
        1
      )
    },
    // 获取省
    getProvince() {
      getProvinceList().then((res) => {
        if (res.code === 200) {
          this.ProvinceList = res.data
        }
      })
    },
    // 获取省的id
    provinceChange(value) {
      this.mxForm.destinationProvince = value.province
      this.getCity(value.pid)
    },
    getCity(pid) {
      getCityId(pid).then((res) => {
        if (res.code === 200) {
          this.cityList = res.data
        }
      })
    },
    // 承包区
    async ShowCoverArea() {
      const res = await ShowCoverAreaApi()
      this.ShowCoverAreaList = res.data
    },
    // 选择时间
    changeTime(value) {
      this.form.startTime = value[0]
      this.form.endTime = value[1]
    },
    table(res) {
      if (res.code === 200) {
        this.tableData = res.data.list.data
        this.total = res.data.list.total
        this.current = res.data.list.current
        this.pages = res.data.list.pages
        this.th = res.data.fields
      } else {
        this.tableData = []
        this.total = 0
      }
    },
    // 查询
    async onSubmit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          this.loading = true
          let res = null
          if (this.id === 1) {
            res = await ToSenderApi(this.form)
            this.loading = false
          } else if (this.id === 2) {
            res = await ExpenseSubitemApi({
              ...this.form,
              channelId: this.channelId,
              settleSubjectId: this.settleSubjectId
            })
            this.loading = false
          }
          this.table(res)
        } else {
          return false
        }
      })
    },
    chan() {
      this.tableData = []
    },
    // 重置
    khClear(aaa) {
      if (aaa === undefined) {
        for (const key in this.form) {
          if (key === 'pageDate') {
            this.form[key] = {
              pageNum: 1,
              pageSize: 10
            }
          } else {
            this.form[key] = ''
          }
        }
        this.channelId = ''
        this.settleSubjectId = ''
        return
      }
      console.log(aaa)
      this.form = aaa
      this.value1 = ''
      this.channelId = ''
      this.settleSubjectId = ''
    },
    // 费用子项列表
    async FeeSubItem() {
      const res = await FeeSubItemApi1()
      this.FeeSubItemList = res.data
    },
    // 结算科目列表
    async Settlement() {
      const res = await SettlementApi()
      this.SettlementList = res.data
    }
  }
}
</script>
<style scoped lang="scss">
.tit {
  display: flex;

  .tex1 {
    margin-right: 48px;
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;
    background: #fff;
    border: 1px #fff solid;
  }

  .active {
    font-family: Source Han Sans CN;
    font-weight: 700;
    color: #2087d0;
    position: relative;
  }

  .active::before {
    position: absolute;
    content: '';
    width: 62px;
    height: 2px;
    background: #2087d0;
    border-radius: 1px;
    bottom: -20px;
    left: 50%;
    transform: translate(-50%);
  }
}

.head {
  padding: 30px;
}

.title {
  font-size: 16px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #666666;
  margin-top: 20px;
}

.line {
  height: 1px;
  background: #d8d8d8;
  border-radius: 1px;
  margin-top: 19px;
}

.title1 {
  // display: flex;
  // justify-content: space-between;
  .text {
    font-size: 18px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;

    // margin-left: 52px;
    .input {
      width: 286px;
      height: 36px;
      background: #ffffff;
      // border: 1px solid #B4B4B4;
      border-radius: 4px;
    }

    .input1 {
      width: 708px;
      height: 36px;
      background: #ffffff;
      border: 1px solid #b4b4b4;
      border-radius: 4px;
    }
  }
}

.title2 {
  font-size: 18px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #666666;
}
</style>
